Hello, |
Re: Howto define a static string array function parameter as call by reference you defined the array as containing no elements. This static feature can't be changed. |
Re: Howto define a static string array function parameter as call by reference
See this to see what you can and can't do:
string arrayNumbers[] = {"One", "Two", "Three", "Four", "Five"};
string arrayColors[] = {"Red", "Blue", "Green"};
string pointArraySomewhere(bool wantColors)[] {
if (wantColors) {
return arrayColors;
} else {
return arrayNumbers;
}
}
//------------------------------------------------------------------------------
// Don't do this.
//
// The syntax is fine but the issue is you will be assigning
// a reference to array storage that will become garbage as soon as the
// function ends.
//
// You can assign arrays in this fashion if and only if the storage stays in
// scope.
//------------------------------------------------------------------------------
string loadMoods()[] {
string moods[] = { "Normal", "DOORS Rage" };
return moods;
}
void main() {
int i;
bool wantColors;
string emptyArray[] = {}
string arrayX[];
wantColors = false;
arrayX = emptyArray;
arrayX = pointArraySomewhere(wantColors);
print "sizeof(arrayX) = " (sizeof(arrayX)) "\n"
for (i = 0; i < sizeof(arrayX); i++) {
print("array[" (i) "] = " arrayX[i] "\n");
}
wantColors = true;
arrayX = emptyArray;
arrayX = pointArraySomewhere(wantColors);
print "sizeof(arrayX) = " (sizeof(arrayX)) "\n"
for (i = 0; i < sizeof(arrayX); i++) {
print("array[" (i) "] = " arrayX[i] "\n");
}
arrayX = emptyArray;
arrayX = loadMoods();
print "sizeof(arrayX) = " (sizeof(arrayX)) "\n"
// If the below will either crash DOORS or not consistently execute
for (i = 0; i < sizeof(arrayX); i++) {
print("array[" (i) "] = " arrayX[i] "\n");
}
}
main();
EXECUTION:
|
Re: Howto define a static string array function parameter as call by reference As Hal already demonstrated it is not a problem of the size of the array, but a problem of assignment:
int ar1[] = {1,2,3}
int ar2[] = {1,2,3,4}
int ar[]
ar = ar2; print ar[3] // prints 4
ar = ar1; print ar[2] // prints 3
The above works. Obviously we can reassign an array. However when we put the assignment to a function, then it does NOT work:
int globalArray[] = {1,2,3}
int ar[]
void func (int &y[]) { y = globalArray }
func (ar); print ar[2] // array bounds exceeded (2)
int globalArray[] = {1,2,3}
int ar[]
void func (int (&y)[]) { y = globalArray }
func (ar); print ar[2] // prints 3
int globalArray[] = {1,2,3}
int ar[]
void func (int (&y)[]) { int temp[3] = {4,5,6}; y = temp}
func (ar); print ar[2] // prints 6
int globalArray[] = {1,2,3}
int ar[]
void func (int (&y)[]) { int temp[3] = {4,5,6}; y = temp }
int useSomeStack(int nr) { if (nr < 1000) return useSomeStack(nr+1) else return nr }
func (ar); // get the temporary array
useSomeStack(0); // use some stack after returning the temp array, before accessing its contents
// print ar[2] // comment me in to crash the program
|
Re: Howto define a static string array function parameter as call by reference thanks a lot to all of you! Helko |